﻿using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;
using Mekiasm.FluidElement;

namespace Mekiasm
{
    public class ElectricPump : ComplexModelItem
    {
        public static int Index;
        public ElectricPump() :
            base(new ModelItem[] {
                new ModelItem("powerConnectorFrame2", "electric_pump"), new ModelItem("powerPort", "electric_pump"),
                new ModelItem("powerConnector", "electric_pump"), new ModelItem("powerConnectorFrame3", "electric_pump"),
                new ModelItem("pumpRingTop", "electric_pump"), new ModelItem("powerConnectorFrame4", "electric_pump"),
                new ModelItem("pumpPortTop", "electric_pump"), new ModelItem("pumpBase", "electric_pump"),
                new ModelItem("powerConnectorFrame1", "electric_pump"),
                new ModelItem("pumpCasing", "electric_pump")
            }, "电动泵", "电动泵")
        {
            
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric | MekBlock.DeviceType.Fluid;
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z,
                new List<ComponentEnergyMachine.Slot>() { new ComponentEnergyMachine.Slot() { FuelCapacity = 20480, SlotType = ComponentEnergyMachine.SlotType.OUT } },
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric)
            }), new MachineAmountLimitConfig() { 
                MaxPower = 10240,
                InputPowerLimit = 32, InputFluidAmountLimit = 32,
                OutputPowerLimit = 32, OutputFluidAmountLimit = 32
            });
            ElectricSystem.Add(new ElectricPumpElement(x, y, z));
            FluidSystem.Add(new ElectricPumpFluidElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new ElectricPumpElement(x, y, z));
            FluidSystem.Add(new ElectricPumpFluidElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            Point3 point = new Point3(x, y, z);
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(point);
            FluidSystem.Remove(point);
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            int id = componentMiner.ActiveBlockValue;
            id = Terrain.ExtractContents(id);
            if (BlocksManager.Blocks[id].IsPlaceable)
            {
                return false;
            }
            ComponentElectricPump mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentElectricPump>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new ElectricPumpWidget(mcomponent, componentMiner);
            return true;
        }
    }
}
